﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyCalender : System.Web.UI.UserControl
{

    public string CurrentDay = "";
    public string CurrentMonth = "";
    public string CurrentYear = "";


    public TextBox TxtDay
    {
        get
        {
            return txtDay;
        }
    }


    public TextBox TxtMonth
    {
        get
        {
            return txtMonth;
        }
    }

    public TextBox TxtYear
    {
        get
        {
            return txtYear;
        }
    }


    public Calendar objCalender
    {
        get
        {
            return BirthDayCal;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentDay = txtDay.Text.Trim();
        CurrentMonth = txtMonth.Text.Trim();
        CurrentYear = txtYear.Text.Trim();

        if (!Page.IsPostBack)
        {
            
            DataBind();
        }
    }



    protected void BirthDayCal_SelectionChanged(object sender, EventArgs e)
    {
        
        showdvCal();


        
        setText(BirthDayCal.SelectedDate);
        
        

    }

    /// <summary>
    /// this function will set the text boxes with date time values
    /// </summary>
    /// <param name="dtNew">the date time to set by</param>
    protected void setText(DateTime dtNew)
    {
        try
        {
            string strDate = dtNew.ToString("dd/MM/yyyy");
            string[] arrDate = strDate.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            txtDay.Text = arrDate[0];
            txtMonth.Text = arrDate[1];
            txtYear.Text = arrDate[2];

            CurrentDay = txtDay.Text.Trim();
            CurrentMonth = txtMonth.Text.Trim();
            CurrentYear = txtYear.Text.Trim();

        }
        catch
        {
            clear();
        }
    }



    protected void BirthDayCal_VisibleMonthChanged(object sender, System.Web.UI.WebControls.MonthChangedEventArgs e)
    {
        showdvCal();
    }




   

    /// <summary>
    /// this function will show the div at page.load
    /// </summary>
    protected void showdvCal()
    {
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "showCal", "<script type=\"text/javascript\">showCalDiv" + this.ClientID.ToString() + "('dvBirthDayCal" + this.ClientID.ToString() + "');</script>", false);





    }


    /// <summary>
    /// this function will clear the text boxes
    /// </summary>
    public void clear()
    {
        txtYear.Text = "";
        txtMonth.Text = "";
        txtDay.Text = "";

        CurrentYear = "";
        CurrentMonth = "";
        CurrentDay = "";
    }



    /// <summary>
    /// this for setting a value programatically
    /// </summary>
    /// <param name="Day">the day</param>
    /// <param name="Month">the month</param>
    /// <param name="Year">the year</param>
    public void set(int Day, int Month, int Year)
    {
        DateTime dtNew ;
        try
        {
            System.Globalization.GregorianCalendar um = new System.Globalization.GregorianCalendar();
            dtNew = um.ToDateTime(Year, Month, Day, 0, 0, 0, 0);
        }
        catch
        {
            clear();
            return;
        }

        setText(dtNew);

        

    }


    /// <summary>
    /// this for setting a value programatically by a date
    /// </summary>
    /// <param name="dtNew">the date to set</param>
    public void set(DateTime dtNew)
    {
        

        setText(dtNew);



    }

    /// <summary>
    /// this function will return the date selected in the form date time
    /// </summary>
    /// <returns>a nullable datetime with the value selected</returns>
    public DateTime? get()
    {
        DateTime dtNew;
        try
        {
            if (CurrentYear == "" && CurrentMonth == "" && CurrentDay == "")
                return null;
            //dtNew = new DateTime(int.Parse(CurrentYear),int.Parse( CurrentMonth), int.Parse(CurrentDay) );

            System.Globalization.GregorianCalendar um = new System.Globalization.GregorianCalendar();
            dtNew =  um.ToDateTime(int.Parse(CurrentYear), int.Parse(CurrentMonth), int.Parse(CurrentDay), 0, 0, 0,0);

        }
        catch (Exception ex)
        {
            RaiseError (new CustomEventArgs (false,ex));
            return null;
        }

        return dtNew;

    }



    /// <summary>
    /// this function will return the date selected in the form date time
    /// </summary>
    /// <param name="dtNew">a date time have the return value</param>
    /// <returns>a bool value has a (true) for a succefull parsing and false for wrong date format</returns>
    public bool get(ref DateTime? dtNew)
    {
        
        try
        {
            if (CurrentYear == "" && CurrentMonth == "" && CurrentDay == "")
            {
                dtNew = null;
                return true;
            }
            //dtNew = new DateTime(int.Parse(CurrentYear),int.Parse( CurrentMonth), int.Parse(CurrentDay) );

            System.Globalization.GregorianCalendar um = new System.Globalization.GregorianCalendar();
            dtNew = um.ToDateTime(int.Parse(CurrentYear), int.Parse(CurrentMonth), int.Parse(CurrentDay), 0, 0, 0, 0);

        }
        catch (Exception ex)
        {
            RaiseError(new CustomEventArgs(false, ex));
            dtNew = null;
            return false;
        }

        return true;

    }



    //this code for raising error in case there is values but not date
    public delegate void onErrorDel(object sender, CustomEventArgs e);
    public event onErrorDel Error = null;


    public void RaiseError(CustomEventArgs e)
    {
        if (Error != null)
            Error(this, e);
    }
    


}
